import pandas as pd
import numpy as np

# 聚合操作
# dataframe聚合函数
# -- 求和
# -- 均值
# -- 最小值
# -- 最大值

date = np.random.randint(1, 100, size=(6,6))
index = [
    ['一班','一班','一班','二班','二班','二班'],
    ['小明','小红','小张','小白','小李','小王']
]
columns = [
    ['期中','期中','期中','期末','期末','期末'],
    ['语文','数学','英语','语文','数学','英语'],
]
df = pd.DataFrame(date,index, columns = columns)
print(df)
print('\n')
print(df.loc['一班', '期末'])
print('\n')
df1=df.loc['一班', '期末']
print(df1.values.sum())
print('\n')
print(df1.sum())
print('\n')
print(df1.sum(axis=0)) # 求每一列中多行的和
print('\n')
print(df1.sum(axis=1)) # 求每一行中多列的和
print('\n')
print(df1.mean()) # 求均值
print('\n')
print(df1.min()) # 求最小值
print('\n')

# 多层索引的聚合操作
date = np.random.randint(1, 100, size=(6,6))
index = [
    ['一班','一班','一班','二班','二班','二班'],
    ['小明','小红','小张','小白','小李','小王']
]
columns = [
    ['期中','期中','期中','期末','期末','期末'],
    ['语文','数学','英语','语文','数学','英语'],
]
df2 = pd.DataFrame(date,index, columns = columns)
print(df2)
s1=df2.groupby(level=0).sum()
print(s1)
s2=df2.groupby(level=1).sum()
print(s2)
s3=df2.groupby(axis=1, level=0).sum()
print(s3)




